import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GradeConverter extends JFrame {
    private JTextField scoreField;
    private JLabel resultLabel;
    private JLabel commentLabel;
    private JButton convertButton;
    public GradeConverter(){
        setTitle("成绩等级转换器");
        setSize(400,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(5,1,10,10));
        setLocationRelativeTo(null);
        JLabel titleLabel = new JLabel("输入你的分数（0-100）", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        scoreField = new JTextField();
        scoreField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        scoreField.setHorizontalAlignment(JTextField.CENTER);
        convertButton = new JButton("转换！");
        convertButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        convertButton.setBackground(new Color(135, 206, 250));
        resultLabel = new JLabel("", JLabel.CENTER);
        resultLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        commentLabel = new JLabel("",JLabel.CENTER);
        commentLabel.setFont(new Font("微软雅黑",Font.ITALIC,14));
        add(titleLabel);
        add(scoreField);
        add(convertButton);
        add(resultLabel);
        add(commentLabel);
        convertButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                convertGrade();
            }
        });
    }
    private void convertGrade() {
        try{
            int score = Integer.parseInt(scoreField.getText());
            if (score < 0 || score > 100) {
                JOptionPane.showMessageDialog(this, "请输入0-100之间的分数！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }
            convertButton.setBackground(Color.PINK);
            Timer timer = new Timer(300, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    convertButton.setBackground(new Color(135, 206, 250));
                }
            });
            timer.setRepeats(false);
            timer.start();
            String grade;
            String comment;
            if (score >= 90) {
                grade = "A";
                comment = "太棒了！继续保持！";
            } else if (score >= 80) {
                grade = "B";
                comment = "很好！再接再厉！";
            } else if (score >= 70) {
                grade = "C";
                comment = "不错，但还有提升空间！";
            } else if (score >= 60) {
                grade = "D";
                comment = "刚好及格，下次要更努力哦！";
            } else {
                grade = "F";
                comment = "别灰心，加油！你可以做到的！";
            }
            resultLabel.setText("等级: " + grade);
            commentLabel.setText(comment);

            switch (grade) {
                case "A":
                    resultLabel.setForeground(Color.RED);
                    break;
                case "B":
                    resultLabel.setForeground(Color.BLUE);
                    break;
                case "C":
                    resultLabel.setForeground(Color.GREEN);
                    break;
                case "D":
                    resultLabel.setForeground(Color.ORANGE);
                    break;
                case "F":
                    resultLabel.setForeground(Color.GRAY);
                    break;
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入有效的数字！", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GradeConverter converter = new GradeConverter();
            converter.setVisible(true);
        });
    }
}
